package com.example.yogurtsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yogurtsystem.common.PageResult;
import com.example.yogurtsystem.dto.ResultDTO;
import com.example.yogurtsystem.entity.Budget;
import com.example.yogurtsystem.mapper.BudgetMapper;
import com.example.yogurtsystem.service.BudgetService;
import com.example.yogurtsystem.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther lecon
 * @date 2021/8/15  13:30
 */
@Service
public class BudgetServiceImpl implements BudgetService {

    @Resource
    BudgetMapper budgetMapper;

    @Override
    public int insertBudgetByYear(int year) {
        QueryWrapper<Budget> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",year);
        int count = budgetMapper.selectCount(queryWrapper);
        if(count != 0){
            return count;
        }

        List<Budget> list = new ArrayList<>(4);
        for (int i = 0; i < 4; i++) {
            Budget budget = new Budget();
            budget.setCreateTime(new Date());
            budget.setUpdateTime(new Date());
            budget.setYear(year);
            budget.setTotalPrice(new BigDecimal(0));
            budget.setSeason(i + 1);
            list.add(budget);
        }

        for (Budget budget: list) {
            budgetMapper.insert(budget);
        }
        return 0;
    }

    @Override
    public void updateBudget(@RequestBody Budget budget) {
        budget.setUpdateTime(new Date());
        budgetMapper.updateById(budget);
    }

    @Override
    public Budget getOne(String id) {
        return budgetMapper.selectById(id);
    }

    @Override
    public List<Budget> getBudget(int year) {
        QueryWrapper<Budget> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",year);
        queryWrapper.orderByAsc("season");
        return budgetMapper.selectList(queryWrapper);
    }

    @Override
    public ResultDTO getBudget(int pageNum, int pageSize, int year) {
        QueryWrapper<Budget> queryWrapper = new QueryWrapper<>();
        if(year != 0){
            queryWrapper.eq("year",year);
            queryWrapper.orderByAsc("season");
        }else{
            queryWrapper.orderByDesc("year","season");
        }
        Page<Budget> page=new Page<>(pageNum,pageSize);
        IPage<Budget> iPage = budgetMapper.selectPage(page, queryWrapper);
        PageResult pageResult = PageUtil.pageResultCopy(iPage);
        return ResultDTO.requestSuccess(pageResult);
    }

    @Override
    public BigDecimal getQuarterBudget(String yearAndQuarter) {
        String year = yearAndQuarter.substring(0, 4);
        String quarter = yearAndQuarter.substring(5, yearAndQuarter.length());
        QueryWrapper<Budget> budgetQueryWrapper = new QueryWrapper<>();
        budgetQueryWrapper.eq("year", year);
        budgetQueryWrapper.eq("season", quarter);
        Budget budget = budgetMapper.selectOne(budgetQueryWrapper);
        return budget.getTotalPrice();
    }


}
